<?php
/**
 * 商品评价
 */

namespace app\admin\model;


use app\common\model\Model;


class GoodsComment extends Model
{
    const TABLE_NAME = 'goods_comment';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

    const COMMENT_IMAGE_SAVE_PATH='goods_comment';
    protected $name = self::TABLE_NAME;


    public static function getCommentQuery($goods_id=null,$user_id=null)
    {
        $query = self::commonQuery(self::TABLE_NAME);

        if(is_numeric($goods_id))
        {
            $query->where(['goods_id'=>$goods_id]);
        }else if(is_array($goods_id))
        {
            $query->where(['goods_id'=>['in',$goods_id]]);
        }

        if(is_numeric($user_id))
        {
            $query->where(['user_id'=>$user_id]);

        }else if(is_array($user_id)){
            $query->where(['user_id'=>['in',$user_id]]);
        }

        return $query;
    }
    //获取 商品评价 和 用户评价
    public static function handleCommentInfo($info)
    {

        $info['user_name'] = UserModel::getUserName($info['user_id']);
        $info['comment_time']=date('Y-m-d H:i',$info['comment_time']);


        if($info['comment_imgs'])
        {
            $images = explode(',',$info['comment_imgs']);
            $info['comment_imgs']=[];
            foreach ($images as $v)
            {
                $info['comment_imgs'][]=get_image_url(self::COMMENT_IMAGE_SAVE_PATH,$v);
            }
        }

        $goods_info = GoodsModel::getGoodsInfoById($info['goods_id']);
        $info['goods_name']=$goods_info['goods_name'];

        return $info;
    }
    public static function deleteComment($comment_id)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['comment_id'=>$comment_id])
               ->delete();
    }


}